package com.wangdao.wechatmall.service.admin;

import com.wangdao.wechatmall.bean.bo.admin.CategoryCreateBO;
import com.wangdao.wechatmall.bean.pojo.Category;
import com.wangdao.wechatmall.bean.pojo.CategoryExample;
import com.wangdao.wechatmall.bean.vo.admin.CategoryVO;
import com.wangdao.wechatmall.bean.vo.admin.L1DataVO;
import com.wangdao.wechatmall.exception.BrandDescException;
import com.wangdao.wechatmall.exception.BrandNameException;
import com.wangdao.wechatmall.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liyongqiang
 * CreateDate 2021/7/9 21:07
 */

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryVO> queryList() {

        List<CategoryVO> categoryVOS = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(0);

        //L1
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setId(category.getId());
            categoryVO.setName(category.getName());
            categoryVO.setKeywords(category.getKeywords());
            categoryVO.setDesc(category.getDesc());
            categoryVO.setIconUrl(category.getIconUrl());
            categoryVO.setPicUrl(category.getPicUrl());
            categoryVO.setLevel(category.getLevel());

            List<CategoryVO.ChildrenDTO> childrenDTOList = new ArrayList<>();
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
            criteria1.andLevelEqualTo("L2").andPidEqualTo(category.getId()).andDeletedEqualTo(0);
            List<Category> category2s = categoryMapper.selectByExample(categoryExample1);
            for (Category category2 : category2s) {
                CategoryVO.ChildrenDTO childrenDTO = new CategoryVO.ChildrenDTO();
                childrenDTO.setId(category2.getId());
                childrenDTO.setName(category2.getName());
                childrenDTO.setKeywords(category2.getKeywords());
                childrenDTO.setDesc(category2.getDesc());
                childrenDTO.setIconUrl(category2.getIconUrl());
                childrenDTO.setPicUrl(category2.getPicUrl());
                childrenDTO.setLevel(category2.getLevel());

                childrenDTOList.add(childrenDTO);
            }
            categoryVO.setChildren(childrenDTOList);
            categoryVOS.add(categoryVO);
        }


        return categoryVOS;
    }

    @Override
    public List<L1DataVO> getL1() {
        List<L1DataVO> l1DataVOS = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(0);

        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            L1DataVO l1DataVO = new L1DataVO();
            l1DataVO.setValue(category.getId());
            l1DataVO.setLabel(category.getName());

            l1DataVOS.add(l1DataVO);
        }

        return l1DataVOS;
    }

    @Override
    public Category create(CategoryCreateBO categoryCreateBO) throws BrandNameException, BrandDescException {
        if (categoryCreateBO.getName().length() > 50){
            throw new BrandNameException();
        }
        if (categoryCreateBO.getDesc().length() > 1000){
            throw new BrandDescException();
        }
        if (categoryCreateBO.getKeywords().length() > 20){
            throw new BrandNameException();
        }
        Category category = new Category();
        category.setName(categoryCreateBO.getName());
        category.setKeywords(categoryCreateBO.getKeywords());
        category.setDesc(categoryCreateBO.getDesc());
        category.setPid(categoryCreateBO.getPid());
        category.setIconUrl(categoryCreateBO.getIconUrl());
        category.setPicUrl(categoryCreateBO.getPicUrl());
        category.setLevel(categoryCreateBO.getLevel());
        category.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        category.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        category.setDeleted(0);

        categoryMapper.insertSelective(category);
        Category category1 = new Category();
        category1.setName(categoryCreateBO.getName());
        category1.setKeywords(categoryCreateBO.getKeywords());
        category1.setDesc(categoryCreateBO.getDesc());
        category1.setPid(categoryCreateBO.getPid());
        category1.setIconUrl(categoryCreateBO.getIconUrl());
        category1.setPicUrl(categoryCreateBO.getPicUrl());
        category1.setLevel(categoryCreateBO.getLevel());
        category1.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        category1.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return category1;
    }

    @Override
    public int update(CategoryVO categoryVO) throws BrandNameException, BrandDescException {

        if (categoryVO.getName().length() > 50){
            throw new BrandNameException();
        }
        if (categoryVO.getDesc().length() > 1000){
            throw new BrandDescException();
        }
        if (categoryVO.getKeywords().length() > 20){
            throw new BrandNameException();
        }

        Category category = new Category();
        Integer id = categoryVO.getId();
        Category category2 = categoryMapper.selectByPrimaryKey(id);
        if (category2.getLevel().equals("L1") && categoryVO.getLevel().equals("L2")){
            return 0;
        }else {
            category.setId(categoryVO.getId());
            category.setName(categoryVO.getName());
            category.setKeywords(categoryVO.getKeywords());
            category.setDesc(categoryVO.getDesc());
            category.setPid(categoryVO.getPid());
            category.setIconUrl(categoryVO.getIconUrl());
            category.setPicUrl(categoryVO.getPicUrl());
            category.setLevel(categoryVO.getLevel());
            categoryMapper.updateByPrimaryKeySelective(category);

            //判断children中是否有数据
            if (categoryVO.getChildren() == null) {
                return 1;
            }
            List<CategoryVO.ChildrenDTO> childrenDTOS = categoryVO.getChildren();
            for (CategoryVO.ChildrenDTO childrenDTO : childrenDTOS) {
                Category category1 = new Category();
                category1.setId(childrenDTO.getId());
                category1.setName(childrenDTO.getName());
                category1.setKeywords(childrenDTO.getKeywords());
                category1.setDesc(childrenDTO.getDesc());
                category1.setPid(categoryVO.getPid());
                category1.setIconUrl(childrenDTO.getIconUrl());
                category1.setPicUrl(childrenDTO.getPicUrl());
                category1.setLevel(childrenDTO.getLevel());
                categoryMapper.updateByPrimaryKeySelective(category1);
            }

            return 1;
        }


    }

    @Override
    public int delete(CategoryVO categoryVO) {
        Category category = new Category();
        if (categoryVO.getLevel().equals("L1")) {

            category.setId(categoryVO.getId());
            category.setDeleted(1);
            categoryMapper.updateByPrimaryKeySelective(category);
            List<CategoryVO.ChildrenDTO> childrenDTOS = categoryVO.getChildren();
            for (CategoryVO.ChildrenDTO childrenDTO : childrenDTOS) {
                Category category1 = new Category();
                category1.setId(childrenDTO.getId());
                category1.setDeleted(1);
                categoryMapper.updateByPrimaryKeySelective(category1);
            }
        } else {
            category.setId(categoryVO.getId());
            category.setDeleted(1);
            categoryMapper.updateByPrimaryKeySelective(category);
        }
        return 1;
    }
}
